/**
 * 实名认证
 */
define(function (require, exports, module) {
    require("jquery.Jcrop");
    var uploadInst = require("ui/upload");


    var UI = require("js/UI");
    var defUserHeadBg = "../Public/image/user_head_bg.png"
    var $userHeadWrap = $(".uploadUserHead .userHeadWrap")

    $userHeadWrap.find("[type=submit]").unbind("click.sub").bind("click.sub", function () {
        var $this = $(this);
        if ($this.hasClass("disable")) {
            return false;
        }
    })


    uploadInst({
        button_image_url: "/Public/image/ui_ico_0030.png",
        button_width: "110",
        button_height: "35",
        uploadSuccess: function (file, serverData) {
            var path = serverData.data.path;
            var $preview = $userHeadWrap.find(".preview div"),
                $preImg = $preview.find("img");


            var $cropImg = $userHeadWrap.find(".crop img").attr("src", path);
            $preImg.attr("src", path);

            $userHeadWrap.find("[type=submit]").removeClass("disable");

            var $txt = $userHeadWrap.find(".txt").hide();
            var $crop = $userHeadWrap.find(".crop").fadeIn();

            var $x = $userHeadWrap.find('#crop_x'),
                $y = $userHeadWrap.find('#crop_y'),
                $w = $userHeadWrap.find('#crop_w'),
                $h = $userHeadWrap.find('#crop_h');

            //重置
            $userHeadWrap.find(".btnWrap .reset").unbind("click.reset").bind("click.reset", function () {
                $x.val("");
                $y.val("");
                $w.val("");
                $h.val("");
                $preImg.removeAttr("style").attr("src", defUserHeadBg)
                $crop.hide();
                $txt.show()
            })

            //<editor-folder desc='头像裁切'>
            var boundx, boundy;
            var xsize = $preview.width(),
                ysize = $preview.height();

            function updatePreview(c) {
                if (parseInt(c.w) > 0) {
                    var rx = xsize / c.w;
                    var ry = ysize / c.h;

                    $preImg.css({
                        width: Math.round(rx * boundx) + 'px',
                        height: Math.round(ry * boundy) + 'px',
                        marginLeft: '-' + Math.round(rx * c.x) + 'px',
                        marginTop: '-' + Math.round(ry * c.y) + 'px'
                    });
                    $userHeadWrap.find('#crop_x').val(c.x);
                    $userHeadWrap.find('#crop_y').val(c.y);
                    $userHeadWrap.find('#crop_w').val(c.w);
                    $userHeadWrap.find('#crop_h').val(c.h);
                }
            };
            $cropImg.Jcrop({
                onChange: updatePreview,
                onSelect: updatePreview,
                bgColor: 'black',
                bgOpacity: .4,
                minSize: [120, 120],
                setSelect: [0, 0, 120, 120],
                aspectRatio: 1
            }, function () {
                var bounds = this.getBounds();
                boundx = bounds[0];
                boundy = bounds[1];
            })
            //</editor-folder>

        }
    })

})